package com.nstar.base.util
{
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.SpreadMethod;
	import flash.geom.Matrix;
	
	import mx.utils.ColorUtil;
	
	public class GraphicsUtils
	{
		public function GraphicsUtils()
		{
		}
		/**
		 * drawSector: 画任意角度的扇形
		 * g		:要在上面绘制的对象.
		 * x,y		:扇形的中心点，即圆心
		 * r		:扇形半径
		 * angle	:扇形的角度
		 * startFrom:扇形角度的起始位置，顺时针方向算起，270 上, 180 左, 0 右, 90 下 
		 * color	:填充颜色
		 */
		public static function drawSector(g:Graphics,x:Number=200,y:Number=200,r:Number=100,
			angle:Number=27,startFrom:Number=270,color:Number=0xff0000):void {
		    
		    g.beginFill(color,50);
		    //remove this line to unfill the sector
		    /* the border of the secetor with color 0xff0000 (red) , you could replace it with any color 
		    * you want like 0x00ff00(green) or 0x0000ff (blue).
		    */
		    g.lineStyle(0,0xff0000);
		    g.moveTo(x,y);
		    angle=(Math.abs(angle)>360)?360:angle;
		    var n:Number=Math.ceil(Math.abs(angle)/45);
		    var angleA:Number=angle/n;
		    angleA=angleA*Math.PI/180;
		    startFrom=startFrom*Math.PI/180;
		    g.lineTo(x+r*Math.cos(startFrom),y+r*Math.sin(startFrom));
		    for (var i=1; i<=n; i++) {
		        startFrom+=angleA;
		        var angleMid=startFrom-angleA/2;
		        var bx=x+r/Math.cos(angleA/2)*Math.cos(angleMid);
		        var by=y+r/Math.cos(angleA/2)*Math.sin(angleMid);
		        var cx=x+r*Math.cos(startFrom);
		        var cy=y+r*Math.sin(startFrom);
		        g.curveTo(bx,by,cx,cy);
		    }
		    if (angle!=360) {
		        g.lineTo(x,y);
		    }
		    g.endFill();// if you want a sector without filling color , please remove this line.
		}
		
		//绘制指定数量的色轮
		//sourceColors:原色
		//count:要绘制的色轮数
		public static function drawColorWheel(g:Graphics,centerX:Number,centerY:Number,radius:Number,
			sourceColors:Array,count:int):void{
			sourceColors = generateColors(sourceColors,count);
			for(var i:int =0;i<sourceColors.length;i++){
				GraphicsUtils.drawSector(g,centerX,centerY,radius,360/count,270+i*30,sourceColors[i]);
			}  
			g.beginFill(0xFFFFFF);
			g.drawCircle(centerX,centerY,50);
			g.endFill();
		}
		
		//根据源颜色数据生成相应数量的色相环所需要的颜色
		private static function generateColors(sourceColors:Array,count:int):Array{
			var colors:Array = new Array();
			//复制源颜色
			for each(var item:uint in sourceColors){
				colors.push(item);
			}
			
			var nextIdx:int = 0;
			for(var i:int=0;i<sourceColors.length;i++){
				nextIdx = i+1;
				if(nextIdx==sourceColors.length)
					nextIdx = 0;
				
				var mixColor:uint = ColorUtils.mixColor(sourceColors[i],sourceColors[nextIdx]);
				colors.push(mixColor);
			}
			colors.sort();//颜色排序
			if(colors.length!=count){
				colors = generateColors(colors,count);
			}
			return colors;
		}
		
		/**
		 * 方法说明 : 画彩色进度条
		 * 参数说明 :
		 * 		g : 要在其上画进度条的对象 
		 * 		bytesLoaded : 已经加载的字节值
		 * 		bytesTotal  : 总共的字节值
		 * 		x			: 进度条的x坐标
		 * 		y			: 进度条的y坐标
		 * 		barWidth	: 进度条的总宽度
		 * 		barHeight	: 进度条的高度
		 * 		colors		: 颜色数组
		 */ 
		public static function drawColorProgressBar(g:Graphics,bytesLoaded:Number,bytesTotal:Number,
			x:Number = 0,y:Number = 0,barWidth:Number=300,barHeight:Number=20,colors:Array=null):void 
		{   
			//颜色块间隔
			var gap:Number = 5;
			//开始位置的偏移值
			g.clear(); 
			g.lineStyle(2,0xFFFFFF,0.5);
			//如果没有提供颜色值，则设置默认的颜色值
			if(!colors){
				colors = [
					0xA6CE39,0x23B14B,0x01AAAD,0x0173BC,0x015AAC,0x5B2D90,
					0xA4228E,0xEE183A,0xF47322,0xF8941D,0xFDB815,0xFFD403
				];				
			}
			//先画背景色
			var w:Number = (barWidth-(colors.length-1)*gap)/colors.length;
			for(var i:int=0;i<colors.length;i++){
				g.beginFill(colors[i]);
				var px:Number = i*(w+gap);
				g.drawRoundRect(x+px,y,w,barHeight,5,5);
				g.endFill();					
			}
			
			g.lineStyle(2,0xFFFFFF,1);
			//算出进度百分比
			var per:Number = Math.floor(colors.length*(bytesLoaded/bytesTotal));
			//通过调亮原始颜色值来表示进度
			for(var i:int=0;i<per;i++){
				g.beginFill(ColorUtil.adjustBrightness(colors[i],80));
				var px:Number = i*w+i*gap;
				g.drawRoundRect(x+px,y,w,barHeight,5,5);
				g.endFill();	
			}
				
		}
		
		//AIR应用下载的进度条渐变色 [0x004176,0x087fb5]，可参考
		public static function drawGradient(g:Graphics,x:Number = 0,y:Number = 0,
			w:Number = 100,h:Number=10,color1:uint = 0x346bac,color2:uint = 0x34b5f9):void{
			var matrix:Matrix=new Matrix(); 
			matrix.createGradientBox(w,h,0); 
			var spreadMethod:String = SpreadMethod.PAD;
			var alphas:Array=[1,1]; 
			var ratios:Array=[0,255]; 
			var colors:Array = [color1,color2];
			g.lineStyle(); 
			g.beginGradientFill(GradientType.LINEAR,colors,alphas,ratios,matrix,spreadMethod);        
			g.drawRect(x,y,w,h); 
			g.endFill();  			
		}
		
	}
}